package org.projetjava.presentation;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.projetjava.metier.model.Lieu;
import org.projetjava.metier.model.Parcours;
import org.projetjava.metier.model.Poi;

public class MapScaleParcours extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	BufferedImage image;
    double scale = 1.0;
    Lieu lieu;
    private ArrayList<JButton> listboutton = new ArrayList<>();
    private ArrayList<Integer> listX = new ArrayList<>();
    private ArrayList<Integer> listY = new ArrayList<>();
 
    public MapScaleParcours(BufferedImage image, Lieu lieu, DescriptionPoi descriptionPoi, Parcours parcours) {
        
    	this.setLayout(null);
    	this.image = image;
        
        this.lieu = lieu;
        for(Poi poi : lieu.getListPoi())
        {
        	int blue = 0;
        	
        	for(Poi poii : parcours.getListPoi())
        	{
        		if(poii.getLibelle().equals(poi.getLibelle()))
        		{
        			blue = 1;
        		}
        	}
        	JButton button;
        	if(blue == 0)
        	{
        		button = new JButton(new ImageIcon("images/gps.png"));
        	}else
        	{
        		button = new JButton(new ImageIcon("images/gps2.png"));
        	}
        	
        	
        	button.setOpaque(false);
        	button.setContentAreaFilled(false);
        	button.setBorderPainted(false);
        	
        	button.setToolTipText(poi.getLibelle());
        	button.addActionListener(new AffichageDescriptionPoi(poi, descriptionPoi));
        	button.setBounds(poi.getX()-20, poi.getY()-40, 40, 40);
        	
        	this.listboutton.add(button);
        	this.listX.add(poi.getX());
        	this.listY.add(poi.getY());

        	this.add(button);
        	
        	//button = null;
        }
    }
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        double x = 0;
        double y = 0;
        AffineTransform at = AffineTransform.getTranslateInstance(x,y);
        at.scale(scale, scale);
        g2.drawRenderedImage(image, at);
        
        for(int i = 0 ; i < listboutton.size(); i++)
        {
        	double tempX = listX.get(i)*scale;
        	double tempY = listY.get(i)*scale;
        	
        	double tempX2 = 20*scale;
        	double tempY2 = 35*scale;
        	
        	listboutton.get(i).setBounds((int)tempX-(int)tempX2, (int)tempY-(int)tempY2, (int)(40*scale), (int)(40*scale));
        }
        
    }
 
    public Dimension getPreferredSize() {
        int w = (int)(scale*image.getWidth());
        int h = (int)(scale*image.getHeight());
        return new Dimension(w, h);
    }
 
    public JSlider getSlider() {
        int min = 1, max = 32, inc = 5;
        final JSlider slider = new JSlider(min, max, 16);
        /*slider.setMajorTickSpacing(5);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setSnapToTicks(true);
        slider.setLabelTable(getLabelTable(min, max, inc));
        slider.setPaintLabels(true);*/
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int value = slider.getValue();
                scale = (value+4)/20.0;
                revalidate();
                repaint();
            }
        });
        return slider;
    }
 
    private Hashtable getLabelTable(int min, int max, int inc) {
        Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
        for(int j = min; j <= max; j += inc) {
            String s = String.format("%.2f", (j+4)/20.0);
            table.put(Integer.valueOf(j), new JLabel(s));
        }
        return table;
    }

}
